﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using AcsCommon;
using YiShi.Class;
using System.Data;
using Microsoft.JScript;

namespace YiShi.Question
{
    public partial class QuestionView : System.Web.UI.Page
    {
        DbAccess db;
        DbEnum de;
        protected string questionType;
        protected string questionBody;
        protected string guideBody;
        protected string guideId;

        public QuestionView()
        {
            db = DbAccess.Get("Admin");
            de = DbEnum.Get(this.db, "Question.Type");

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            JsonObject jo = new JsonObject();
            try
            {
                string action = Request["ActionType"];
                if (string.IsNullOrEmpty(action))
                    return;
                switch (action.ToLower())
                {
                    case "view":
                        View();
                        break;
                    case "next":
                        jo = Next();
                        break;
                }
            }
            catch (Exception e1)
            {
                jo.Add("IsSuccess", "false");
                jo.Add("Message", e1.Message);
                jo.Add("Source", e1.Source);
                jo.Add("StackTrace", e1.StackTrace);
            }
            finally
            {
                if (jo.Count > 0)
                {
                    Response.Write(jo.ToString().Replace("'", "\""));
                    Response.End();
                }
            }
        }
        private JsonObject Next()
        {
            JsonObject jo = new JsonObject();
            string guideid = this.Request["GuideId"];
            Guide g = new Guide(db);
            g.FillFromDb(guideid);
            string answer = this.Request["Answer"];
            GuideLink gl = new GuideLink(this.db);
            switch (g.Type)
            {
                case "SingleSelect":
                    gl.FillFromDb(guideid, answer);
                    break;
                case "FillsUp":
                    if (g.Answer == answer)
                        gl.FillFromDb(guideid, "对");
                    else
                        gl.FillFromDb(guideid, "错");
                    break;
            }


            jo.Add("Tip", gl.Tip);
            if (gl.NextGuideId == "00000000")
                jo.Add("IsOver", "true");
            else
            {
                jo.Add("IsOver", "false");
                if (gl.PreGuideId == gl.NextGuideId)
                    jo.Add("IsNewGuide", "false");
                else
                {
                    jo.Add("IsNewGuide", "true");
                    Guide g1 = new Guide(db);
                    g1.FillFromDb(gl.NextGuideId);
                    jo.Add("GuideId", g1.Id);
                    jo.Add("GuideBody", GlobalObject.escape(GetGuideBody(g1)));
                }
            }
            jo.Add("IsSuccess", "true");
            return jo;
        }

        private void View()
        {
            string id = Request["Id"];
            Class.Question q = new YiShi.Class.Question(this.db);
            q.FillFromDb(id);
            questionType = de.GetText(q.Type);
            questionBody = Utility.HtmlDecode(GlobalObject.unescape(q.Body)).Replace("~0~", "<input style='width:50px' disabled>");
            string sql = Sql.Format("select * from Guide where (QuestionId = '{0}') AND (Name = '主引导')", id);
            DataRow dr = db.QueryDataRow(sql);
            Guide g = new Guide(db);
            dr.FillObject(g);
            guideBody = GetGuideBody(g);
            guideId = g.Id;
        }


        public string GetGuideBody(Guide guide)
        {
            string guidebody = GlobalObject.unescape(guide.Body);
            GuideLink[] links = guide.GetGuideLinks();
            switch (guide.Type)
            {
                case "SingleSelect":
                    string options = "";
                    for (int i = 0; i < links.Length; i++)
                    {
                        options += String.Format("<option value='{0}'>{0}</option>", links[i].Answer);
                    }
                    guidebody = guidebody.Replace("~0~", "<select name='Answer'>" + options + "</select>");
                    break;
                case "FillsUp":
                    byte index = 0;
                    while (guidebody.IndexOf(String.Format("~{0}~", index.ToString())) != -1)
                    {
                        guidebody = guidebody.Replace(String.Format("~{0}~", index.ToString()), String.Format("<input style='width:100px' name='Answer'>"));

                        index++;
                    }
                    break;
            }
            return guidebody.Replace("\n", "<br>");
        }
    }
}
